﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class _Default : System.Web.UI.Page 
{
    private static int error=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
            string user = tuser.Text;
            string pass = tpass.Text;
            string datos="";
            // string webser=Utility.setXml(user,Utility.cifrado(tpass.Text),1,1);

            int valperdil = int.Parse(cbperfil.SelectedValue);
            string webser = "";

            if (valperdil == 0)
            {
                datos = null;
                error = 1;


            }

            try
            {
                webser = Utility.setXml(user, tpass.Text, 1, valperdil);
                com.bpophotos.www.Service service = new com.bpophotos.www.Service();
                datos = service.ValidaUsuario(webser);
            } catch(Exception s){
                Utility.writeError(s.Message);
            }

            if (datos != null|| datos!="")
            {

                try
                {
                    rUser ruser = new rUser();
                    ruser = Utility.getRUser(datos);
                    if (ruser != null)
                    {
                        if (ruser.getRespuestaId() == 1)
                        {

                            Session["userid"] = ruser;
                            Response.Redirect("admin//Workspace.aspx");

                        }
                        else
                        {
                            error = 2;

                        }
                    }

                    else
                    {
                        error = 3;

                    }
                }
                catch (Exception xmlexc)
                {
                    Utility.writeError(xmlexc.StackTrace);
                }

            }
            if (error == 1)
            {// selectgroup
                Response.Write("<" + "script language='javascript'>");
                Response.Write("alert('Login denied. Please select group');");
                Response.Write("<" + "/script>");
                lbnotautenticate.Text = "Invalid Group";
            }
            if (error == 2)
            {
                Response.Write("<" + "script language='javascript'>");
                Response.Write("alert('Login denied. Please enter valid user, password or group');");
                Response.Write("<" + "/script>");

            }
            if (error == 3)
            {
                Response.Write("<" + "script language='javascript'>");
                Response.Write("alert('Login Fail');");
                Response.Write("<" + "/script>");
                Response.Redirect("Default.aspx");
            }
        }
    
}
